package org.lq.ht.view.room;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.JLabel;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;

public class V_Room extends JFrame  {

	private JPanel contentPane;
	private static JPanel panel;
	private static JPanel this_panle;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_Room frame = new V_Room();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_Room() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JPopupMenu popupMenu = new JPopupMenu();
		
		JMenuItem mntmNewMenuItem1 = new JMenuItem("入住");
		popupMenu.add(mntmNewMenuItem1);
		JMenuItem mntmNewMenuItem2 = new JMenuItem("退房");
		popupMenu.add(mntmNewMenuItem2);
		JMenuItem mntmNewMenuItem3 = new JMenuItem("详细信息");
		popupMenu.add(mntmNewMenuItem3);
		int y = 10;
		int x = 10;
		for (int i = 1; i <= 35; i++) {
			
			JPanel panel_1 = new JPanel();
			panel_1.setToolTipText(i+"");
			panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
			panel_1.setBounds(x, y, 60, 60);
			panel_1.setBackground(Color.GREEN);
			panel.add(panel_1);
			panel_1.setLayout(null);
			JLabel label = new JLabel("10"+i);
			label.setBounds(10, 10, 54, 15);
			panel_1.add(label);
			JLabel label_1 = new JLabel("标间");
			label_1.setBounds(10, 35, 54, 15);
			panel_1.add(label_1);
			
			if(i %10 == 0) {
				y+=70;
				x = 10;
			}else {
				x += 70;
			}
			
			addPopup(panel_1, popupMenu);
		}
		
		
	}
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				 this_panle = (JPanel) e.getSource();
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
